//
//  CellForInformationVCSectionZero.m
//  echo
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CellForInformationVCSectionZero.h"
#import <DKNightVersion.h>
#import "ModelForInformationVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Social/Social.h>

@interface CellForInformationVCSectionZero ()
@property (nonatomic, retain)UIView *backView;
@property (nonatomic, retain)UIImageView *imageForXingZuo;
@property (nonatomic, retain)UILabel *labelForXingZuo;
@property (nonatomic, retain)UIImageView *imageForLocation;
@property (nonatomic, retain)UILabel *labelForLocation;
@property (nonatomic, retain)UIImageView *imageForCareer;
@property (nonatomic, retain)UILabel *labelForCareerLeft;
@property (nonatomic, retain)UILabel *labelForCareerRight;
@property (nonatomic, retain)UILabel *labelForBottom;
@property (nonatomic, retain)UILabel *labelForAttentioned;
@property (nonatomic, retain)UILabel *labelForAttention;
@property (nonatomic, retain)UIButton *labelForOthers;
@property (nonatomic, retain)UILabel *labelForContent;
@property (nonatomic, retain)UILabel *labelForLeftLine;
@property (nonatomic, retain)UILabel *labelForRightLine;
@property (nonatomic, retain)UILabel *labelForBottomLine;
@property (nonatomic, retain)NSString *strForContontLaber;
@end
@implementation CellForInformationVCSectionZero

- (void)dealloc{
    [_backView release];
    [_imageForXingZuo release];
    [_labelForXingZuo release];
    [_imageForLocation release];
    [_labelForLocation release];
    [_imageForCareer release];
    [_labelForCareerLeft release];
    [_labelForCareerRight release];
    [_labelForBottom release];
    [_labelForAttentioned release];
    [_labelForAttention release];
    [_labelForOthers release];
    [_labelForContent release];
    [_labelForLeftLine release];
    [_labelForRightLine release];
    [_labelForBottomLine release];
    [_strForContontLaber release];
    [_composeVc release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[UIView alloc]init];
        [self.contentView addSubview:self.backView];
        [self.backView release];
    
        self.imageForXingZuo = [[UIImageView alloc]init];
        [self.backView addSubview:self.imageForXingZuo];
        [self.imageForXingZuo release];
        
        self.labelForXingZuo = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForXingZuo];
        [self.labelForXingZuo release];
        self.labelForXingZuo.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.imageForLocation = [[UIImageView alloc]init];
        [self.backView addSubview:self.imageForLocation];
        [self.imageForLocation release];
        
        self.labelForLocation = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForLocation];
        [self.labelForLocation release];
        self.labelForLocation.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.imageForCareer = [[UIImageView alloc]init];
        [self.backView addSubview:self.imageForCareer];
        [self.imageForCareer release];
        
        self.labelForCareerLeft = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForCareerLeft];
        [self.labelForCareerLeft release];
        self.labelForCareerLeft.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.labelForCareerRight = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForCareerRight];
        [self.labelForCareerRight release];
        self.labelForCareerRight.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.labelForBottom = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForBottom];
        [self.labelForBottom release];
        self.labelForBottom.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.labelForAttentioned = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForAttentioned];
        [self.labelForAttentioned release];
        self.labelForAttentioned.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.labelForAttention = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForAttention];
        [self.labelForAttention release];
        self.labelForAttention.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        // 分享按钮
        self.labelForOthers = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backView addSubview:self.labelForOthers];
        
        [self.labelForOthers addTarget:self action:@selector(handleCollectForInformation) forControlEvents:UIControlEventTouchUpInside];
        
        self.labelForContent = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForContent];
        [self.labelForContent release];
        self.labelForContent.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        
        self.labelForLeftLine = [[UILabel alloc]init];
        self.labelForLeftLine.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.backView addSubview:self.labelForLeftLine];
        [self.labelForLeftLine release];
        
        self.labelForRightLine = [[UILabel alloc]init];
        self.labelForRightLine.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.backView addSubview:self.labelForRightLine];
        [self.labelForRightLine release];
        
        self.labelForBottomLine = [[UILabel alloc]init];
        self.labelForBottomLine.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.backView addSubview:self.labelForBottomLine];
        [self.labelForBottomLine release];
        [self handleNightModel];
        
    }
    return self;
}

- (void)handleCollectForInformation{
    NSLog(@"收藏");
    // 1.判断平台是否可用(系统没有集成,用户设置账号)
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"设置界面设置自己的账号");
        
        NSURL *url = [NSURL URLWithString:@"prefs:root="];
        if ([[UIApplication sharedApplication] canOpenURL:url])
        {
            [[UIApplication sharedApplication] openURL:url];
        }
        
        return;
    }
    
    // 2.创建分享控制器
    self.composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 2.1.添加分享的文字
    [self.composeVc setInitialText:self.nameForCollect];
    
    // 2.2.添加分享的图片
    UIImageView *image = [[UIImageView alloc]init];
    [image sd_setImageWithURL:[NSURL URLWithString:self.picForCollect]];
//    [composeVc addImage:[UIImage imageNamed:@"8bc3dd5c103853439da2910c9213b07ecb808893.jpg"]];
    [self.composeVc addImage:image.image];
    // 3.弹出控制器进行分享
    if ([self.delegate respondsToSelector:@selector(presentComposeVc:)]) {
        
        [self.delegate presentComposeVc:self.composeVc];
    }
//    [ presentViewController:composeVc animated:YES completion:nil];
    
    // 4.设置监听发送结果
   self.composeVc.completionHandler = ^(SLComposeViewControllerResult reulst) {
        if (reulst == SLComposeViewControllerResultDone) {
            NSLog(@"用户发送成功");
        } else {
            NSLog(@"用户发送失败");
        }
    };
}

- (void)passModelForInformationCell:(ModelForInformationVC *)model{
    if ([model.constellation_name isEqualToString:@""] && [model.constellation_name isEqualToString:@""]) {

        self.labelForXingZuo.text = @"未填写";
        self.labelForLocation.text = @"未填写";
    }else if ([model.constellation_name isEqualToString:@""] && ![model.city isEqualToString:@""]){

        self.labelForXingZuo.text = @"未填写";
        self.labelForLocation.text = model.city;
    }else if (![model.constellation_name isEqualToString:@""] && [model.city isEqualToString:@""]){
        self.labelForXingZuo.text = model.constellation_name;
        self.labelForLocation.text = @"未填写";
    }else{
        self.labelForXingZuo.text = model.constellation_name;
        self.labelForLocation.text = model.city;
    }
    [self.imageForCareer sd_setImageWithURL:[NSURL URLWithString:model.famous_icon]];
    self.labelForCareerLeft.text = model.famous_type_title;
    self.labelForCareerRight.text = model.famous_sub_type_title;
    self.labelForBottom.text = model.job_title;
    
    self.labelForAttentioned.text = [model.followers_count stringValue];
    self.labelForAttention.text = [model.following_count stringValue];
    [self.labelForOthers setTitle:@"分享" forState:UIControlStateNormal];
    self.labelForContent.text = model.intro;
    self.strForContontLaber = model.intro;
    NSString *str = [model.gender stringValue];
    
    self.picForCollect = model.avatar;
    self.nameForCollect = model.name;
    if ([str isEqualToString:@"1"]) {
        self.imageForXingZuo.image = [UIImage imageNamed:@"iconfont-nv"];
    }else{
        self.imageForXingZuo.image = [UIImage imageNamed:@"iconfont-nan"];
    }
    self.imageForLocation.image = [UIImage imageNamed:@"iconfont-location"];
 
    [self.imageForCareer sd_setImageWithURL:[NSURL URLWithString:model.famous_icon]];
    
}

- (void)handleNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.backView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    
}

- (void)layoutSubviews{
    self.backView.frame = self.contentView.frame;
    
    self.imageForXingZuo.frame = CGRectMake(31 * WIDTH, 18 * HEIGHT, 25 * WIDTH, 25 * HEIGHT);
    
    self.labelForXingZuo.frame = CGRectMake(59 * WIDTH, 15 * HEIGHT, 50 * WIDTH, 30 * HEIGHT);
    self.labelForXingZuo.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForXingZuo.textAlignment = NSTextAlignmentCenter;
    
    self.imageForLocation.frame = CGRectMake(68 * WIDTH + 50 * WIDTH + 10 * WIDTH, 18 * HEIGHT, 25 * WIDTH, 25 * HEIGHT);
    
    self.labelForLocation.frame = CGRectMake(68 * WIDTH + 50 * WIDTH + 10 * WIDTH + 20 * WIDTH, 15 * HEIGHT, 50 * WIDTH, 30 * HEIGHT);
    self.labelForLocation.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForLocation.textAlignment = NSTextAlignmentCenter;
    
    self.imageForCareer.frame = CGRectMake(68 * WIDTH + 50 * WIDTH + 10 * WIDTH + 20 * WIDTH + 5 * WIDTH + 50 * WIDTH + 10 * WIDTH + 7 * WIDTH, 21 * HEIGHT, 21 * WIDTH, 21 * HEIGHT);
    
    self.labelForCareerLeft.frame = CGRectMake(70 * WIDTH + 50 * WIDTH + 10 * WIDTH + 20 * WIDTH + 5 * WIDTH + 50 * WIDTH + 10 * WIDTH + 30 * WIDTH, 16 * HEIGHT, 40 * WIDTH, 30 * HEIGHT);
    self.labelForCareerLeft.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForCareerLeft.textAlignment = NSTextAlignmentCenter;
    self.labelForCareerRight.frame = CGRectMake(68 * WIDTH + 50 * WIDTH + 10 * WIDTH + 25 * WIDTH + 50 * WIDTH + 5 * WIDTH + 30 * WIDTH + 40 * WIDTH, 16 * HEIGHT, 105 * WIDTH, 30 * HEIGHT);
    self.labelForCareerRight.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForCareerRight.textAlignment = NSTextAlignmentCenter;
    
    self.labelForBottom.frame = CGRectMake(self.contentView.frame.size.width / 2 - 100 * WIDTH, 60 * HEIGHT, 200, 30 * HEIGHT);
    self.labelForBottom.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForBottom.textAlignment = NSTextAlignmentCenter;
    
    self.labelForAttentioned.frame = CGRectMake(30 * WIDTH, 105 * HEIGHT, (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3, 45 * HEIGHT);
    self.labelForAttentioned.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForAttentioned.textAlignment = NSTextAlignmentCenter;
    
    self.labelForLeftLine.frame = CGRectMake(30 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH, 108 * HEIGHT, 1.5 * WIDTH, 39 * HEIGHT);
    
    self.labelForAttention.frame = CGRectMake(30 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH + 1.5 * WIDTH + 5 * WIDTH, 105 * HEIGHT, (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3, 45 * HEIGHT);
    self.labelForAttention.font = [UIFont fontWithName:@"Arial" size:15.0];
    self.labelForAttention.textAlignment = NSTextAlignmentCenter;
    
    self.labelForRightLine.frame = CGRectMake(30 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH + 1.5 * WIDTH + 5 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH, 108 * HEIGHT, 1.5 * WIDTH, 39 * HEIGHT);
    
    self.labelForOthers.frame = CGRectMake(30 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH + 1.5 * WIDTH + 5 * WIDTH + (self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3 + 5 * WIDTH + 1.5 * WIDTH + 5 * WIDTH, 105 * HEIGHT,(self.contentView.frame.size.width - 60 * WIDTH - 23 * WIDTH) / 3, 45 * HEIGHT);
    self.labelForOthers.dk_tintColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);

    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        [self.labelForOthers setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        
        [DKNightVersionManager dawnComing];
        [self.labelForOthers setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    self.labelForBottomLine.frame = CGRectMake(0, 105 * HEIGHT + 45 * HEIGHT + 15 * HEIGHT, self.contentView.frame.size.width, 1 * HEIGHT);

    self.labelForContent.numberOfLines = 0;

    self.labelForContent.textAlignment = NSTextAlignmentCenter;
 
    self.labelForContent.font = [UIFont fontWithName:@"Arial" size:16.0];
    self.labelForContent.lineBreakMode = NSLineBreakByWordWrapping;
    self.labelForContent.frame = CGRectMake(40 * WIDTH, 110 * HEIGHT + 45 * HEIGHT + 25 * HEIGHT, self.frame.size.width - 80 * WIDTH, [[self class] heightForContentLabel:self.strForContontLaber]);
}

+ (CGFloat)heightForContentLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 80 * WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:16.0]} context:nil];
    
    return rect.size.height;
}

- (CGFloat)heightForCell:(ModelForInformationVC *)model{
    
    CGFloat height = [CellForInformationVCSectionZero heightForContentLabel:model.intro];
    if (model.intro != nil) {
        
        return height + (110 + 45 + 20 + 5) * HEIGHT;
    }else{
        
        return (110 + 45 + 15) * HEIGHT;
    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
